package com.datahandlers;

import com.gui.helper.CustomTableModel;
//import com.inventory.controllers.ItemControllerRemote;
import com.inventory.entities.Item;
import com.util.ResourceLoader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author sithum
 */
public class ItemDataHandler implements DataHandler {

    private final String COLUMN0 = ResourceLoader.getInstance().getProperty("item.column0");
    private final String COLUMN1 = ResourceLoader.getInstance().getProperty("item.column1");
    private final String COLUMN2 = ResourceLoader.getInstance().getProperty("item.column2");
    private final String COLUMN3 = ResourceLoader.getInstance().getProperty("item.column3");
    private final String COLUMN4 = ResourceLoader.getInstance().getProperty("item.column4");
    private final String COLUMN5 = ResourceLoader.getInstance().getProperty("item.column5");
    private final String COLUMN6 = ResourceLoader.getInstance().getProperty("item.column6");
    private final String COLUMN7 = ResourceLoader.getInstance().getProperty("item.column7");
    private final String COLUMN8 = ResourceLoader.getInstance().getProperty("item.column8");
    private List<Item> itemList;
    private String[] tableHeaders = new String[]{COLUMN0, COLUMN1, COLUMN2, COLUMN3, COLUMN4, COLUMN5, COLUMN6, COLUMN7, COLUMN8, new String()};
    private CustomTableModel model;
    private Object[][] tableData;
//    private ItemControllerRemote controller;
    private String[] itemNames;
    private Map<String, String> editItemMap;
    private List<Item> data;

    public ItemDataHandler(Object cotroller) {
        try {
            this.init(cotroller);
        } catch (Exception ex) {
            Logger.getLogger(ItemDataHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void init(Object controller) throws Exception {
//        this.controller = (ItemControllerRemote) controller;
        itemList = new ArrayList<Item>();
        editItemMap = new HashMap<String, String>();
        this.processData();
        setModel(new CustomTableModel(tableHeaders, getTableData()));
    }

    public void addRecord(Object object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void saveCategories() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * Process the data retrived from the database to display in a JTable.
     * @return java.lang.Object[][]
     * @throws java.lang.Exception
     */
    private void processData() throws Exception {
        //setData(controller.findAll());
        //this.processData(getData());
    }

    private void processData(List<Item> data) {
        Object[][] dataSet = new Object[data.size()][Item.class.getDeclaredFields().length];
        String[] names = new String[data.size()];
        int index = 0;
        for (Item item : data) {
            dataSet[index][0] = item.getItemCode();
            dataSet[index][1] = item.getItemName();
            dataSet[index][2] = new Boolean(item.getDiscountStatus());
            dataSet[index][3] = new Boolean(item.getFreeIssueStatus());
            dataSet[index][4] = item.getPrice();
//            dataSet[index][5] = item.getQohUnits();
//            dataSet[index][6] = item.getQohSubUnits();
//            dataSet[index][7] = item.getReorderLevel();
//            dataSet[index][8] = item.getCategory().getCategoryName();
//            index++;
        }
        this.setTableData(dataSet);
    }

    /**
     * @return the data
     */
    public List<Item> getData() {
        return data;
    }

    /**
     * @param data the data to set
     */
    public void setData(List<Item> data) {
        this.data = data;
    }

    /**
     * @return the tableData
     */
    public Object[][] getTableData() {
        return tableData;
    }

    /**
     * @param tableData the tableData to set
     */
    public void setTableData(Object[][] tableData) {
        this.tableData = tableData;
    }

    /**
     * @return the model
     */
    public CustomTableModel getModel() {
        return model;
    }

    /**
     * @param model the model to set
     */
    public void setModel(CustomTableModel model) {
        this.model = model;
    }

    public void saveData() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean temporaryStorageEmpty() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void addEditRecord(Object key, Object categoryName) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void editData() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean tempEditStorageEmpty() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
